package com.tianji.learning.mq;


import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class LessonChangeListener {
    private final ILearningLessonService lessonService;

    //交换机，路由器，key
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "learning.lesson.pay.queue", durable = "true"),
                    exchange = @Exchange(value = MqConstants.Exchange.ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),
                    key = MqConstants.Key.ORDER_REFUND_KEY
            )
    )
    public void listenLessonKey(OrderBasicDTO dto) {
        //1.判断
        if (dto.getUserId() == null || CollUtils.isEmpty(dto.getCourseIds())) {
            log.error("dto数据有误");
            return;
        }
        lessonService.addLesson(dto);
    }
}
